#include "juzhen.h"
#include "ui_juzhen.h"
#include <QMessageBox>
#include <QTableWidget>
#include <QPushButton>
#include <QLineEdit>
#include <QVBoxLayout>
#include <QStringList>
#include <QVector>
#include <QInputDialog>
#include <QTextEdit>
#include <QString>
juzhen::juzhen(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::juzhen)
{
    ui->setupUi(this);
}

juzhen::~juzhen()
{
    delete ui;
}
void juzhen::on_spinBox_matrix_row_1_valueChanged(int arg1)
{
    ui->tableWidget_matrix_1->setRowCount(arg1);
}


void juzhen::on_spinBox_matrix_column_1_valueChanged(int arg1)
{
    ui->tableWidget_matrix_1->setColumnCount(arg1);
}


void juzhen::on_create_matrix_1_clicked()
{
    int row = ui->spinBox_matrix_row_1->value();
    int col = ui->spinBox_matrix_column_1->value();

    for (int i = 0; i < row; ++i) {
        for (int j = 0; j < col; ++j) {
            ui->tableWidget_matrix_1->setItem(i, j, new QTableWidgetItem("0"));
        }
    }
}


void juzhen::on_tableWidget_matrix_1_cellChanged(int row, int column)
{

}


void juzhen::on_spinBox_matrix_row_2_valueChanged(int arg1)
{
    ui->tableWidget_matrix_2->setRowCount(arg1);
}


void juzhen::on_spinBox_matrix_column_2_valueChanged(int arg1)
{
    ui->tableWidget_matrix_2->setColumnCount(arg1);
}


void juzhen::on_create_matrix_2_clicked()
{
    int row = ui->spinBox_matrix_row_2->value();
    int col = ui->spinBox_matrix_column_2->value();

    for (int i = 0; i < row; ++i) {
        for (int j = 0; j < col; ++j) {
            ui->tableWidget_matrix_2->setItem(i, j, new QTableWidgetItem("0"));
        }
    }
}


void juzhen::on_tableWidget_matrix_2_cellChanged(int row, int column)
{

}
void juzhen::on_multiply_button_clicked()
{
    int row1 = ui->spinBox_matrix_row_1->value();
    int col1 = ui->spinBox_matrix_column_1->value();
    int row2 = ui->spinBox_matrix_row_2->value();
    int col2 = ui->spinBox_matrix_column_2->value();
    if (col1 != row2) {
        QMessageBox::warning(this, "错误", "矩阵相乘，矩阵1的列数必须=矩阵2的行数！");
        return;
    }

    QVector<QVector<double>> matrix1;
    QVector<QVector<double>> matrix2;

    for (int i = 0; i < row1; ++i) {
        QVector<double> row;
        for (int j = 0; j < col1; ++j) {
            row.append(ui->tableWidget_matrix_1->item(i, j)->text().toDouble());
        }
        matrix1.append(row);
    }

    for (int i = 0; i < row2; ++i) {
        QVector<double> row;
        for (int j = 0; j < col2; ++j) {
            row.append(ui->tableWidget_matrix_2->item(i, j)->text().toDouble());
        }
        matrix2.append(row);
    }

    QVector<QVector<double>> result(row1, QVector<double>(col2, 0));

    for (int i = 0; i < row1; ++i) {
        for (int j = 0; j < col2; ++j) {
            for (int k = 0; k < col1; ++k) {
                result[i][j] += matrix1[i][k] * matrix2[k][j];
            }
        }
    }
    // 显示结果
    ui->tableWidget_matrix_answer->setRowCount(row1);
    ui->tableWidget_matrix_answer->setColumnCount(col2);
    for (int i = 0; i < row1; ++i) {
        for (int j = 0; j < col2; ++j) {
            ui->tableWidget_matrix_answer->setItem(i, j, new QTableWidgetItem(QString::number(result[i][j])));
        }
    }
}
void juzhen::on_tableWidget_matrix_answer_cellChanged(int row, int column)
{

}


void juzhen::on_add_button_clicked()
{
    int row1 = ui->spinBox_matrix_row_1->value();
    int col1 = ui->spinBox_matrix_column_1->value();
    int row2 = ui->spinBox_matrix_row_2->value();
    int col2 = ui->spinBox_matrix_column_2->value();
    if (row1 != row2||col1!=col2) {
        QMessageBox::warning(this, "错误", "同型矩阵才能相加！");
        return;
    }

    QVector<QVector<double>> matrix1;
    QVector<QVector<double>> matrix2;

    for (int i = 0; i < row1; ++i) {
        QVector<double> row;
        for (int j = 0; j < col1; ++j) {
            row.append(ui->tableWidget_matrix_1->item(i, j)->text().toDouble());
        }
        matrix1.append(row);
    }

    for (int i = 0; i < row2; ++i) {
        QVector<double> row;
        for (int j = 0; j < col2; ++j) {
            row.append(ui->tableWidget_matrix_2->item(i, j)->text().toDouble());
        }
        matrix2.append(row);
    }

    QVector<QVector<double>> result(row1, QVector<double>(col2, 0));

    for (int i = 0; i < row1; ++i) {
        for (int j = 0; j < col2; ++j) {
                result[i][j] = matrix1[i][j] + matrix2[i][j];
        }
    }
    // 显示结果
    ui->tableWidget_matrix_answer->setRowCount(row1);
    ui->tableWidget_matrix_answer->setColumnCount(col2);
    for (int i = 0; i < row1; ++i) {
        for (int j = 0; j < col2; ++j) {
            ui->tableWidget_matrix_answer->setItem(i, j, new QTableWidgetItem(QString::number(result[i][j])));
        }
    }
}


void juzhen::on_subtrac_button_clicked()
{
    int row1 = ui->spinBox_matrix_row_1->value();
    int col1 = ui->spinBox_matrix_column_1->value();
    int row2 = ui->spinBox_matrix_row_2->value();
    int col2 = ui->spinBox_matrix_column_2->value();
    if (row1 != row2||col1!=col2) {
        QMessageBox::warning(this, "错误", "同型矩阵才能相减！");
        return;
    }

    QVector<QVector<double>> matrix1;
    QVector<QVector<double>> matrix2;

    for (int i = 0; i < row1; ++i) {
        QVector<double> row;
        for (int j = 0; j < col1; ++j) {
            row.append(ui->tableWidget_matrix_1->item(i, j)->text().toDouble());
        }
        matrix1.append(row);
    }

    for (int i = 0; i < row2; ++i) {
        QVector<double> row;
        for (int j = 0; j < col2; ++j) {
            row.append(ui->tableWidget_matrix_2->item(i, j)->text().toDouble());
        }
        matrix2.append(row);
    }

    QVector<QVector<double>> result(row1, QVector<double>(col2, 0));

    for (int i = 0; i < row1; ++i) {
        for (int j = 0; j < col2; ++j) {
            result[i][j] = matrix1[i][j] - matrix2[i][j];
        }
    }
    // 显示结果
    ui->tableWidget_matrix_answer->setRowCount(row1);
    ui->tableWidget_matrix_answer->setColumnCount(col2);
    for (int i = 0; i < row1; ++i) {
        for (int j = 0; j < col2; ++j) {
            ui->tableWidget_matrix_answer->setItem(i, j, new QTableWidgetItem(QString::number(result[i][j])));
        }
    }
}

void juzhen::on_Determinant_clicked()
{
    int row1 = ui->spinBox_matrix_row_1->value();
    int col1 = ui->spinBox_matrix_column_1->value();
    if (row1 != col1) {
        QMessageBox::warning(this, "错误", "只有方阵才有行列式！");
        return;
    }

    QVector<QVector<double>> matrix1;

    for (int i = 0; i < row1; ++i) {
        QVector<double> row;
        for (int j = 0; j < col1; ++j) {
            row.append(ui->tableWidget_matrix_1->item(i, j)->text().toDouble());
        }
        matrix1.append(row);
    }
    double determinant = calculateDeterminant(matrix1);

    // 显示结果
    ui->tableWidget_matrix_answer->setRowCount(1);
    ui->tableWidget_matrix_answer->setColumnCount(1);
    ui->tableWidget_matrix_answer->setItem(0, 0, new QTableWidgetItem(QString::number(determinant)));
}
double juzhen::calculateDeterminant(const QVector<QVector<double>>& matrix)
{
    int n = matrix.size();
    if (n == 1) {
        return matrix[0][0];
    } else if (n == 2) {
        return matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
    } else {
        double determinant = 0;
        for (int i = 0; i < n; ++i) {
            QVector<QVector<double>> subMatrix;
            for (int j = 1; j < n; ++j) {
                QVector<double> row;
                for (int k = 0; k < n; ++k) {
                    if (k == i) continue;
                    row.append(matrix[j][k]);
                }
                subMatrix.append(row);
            }
            determinant += (i % 2 == 0 ? 1 : -1) * matrix[0][i] * calculateDeterminant(subMatrix);
        }
        return determinant;
    }
}

